package com.myspace.service.impl.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myspace.mapper.RecordMapper;
import com.myspace.mapper.UserMapper;
import com.myspace.pojo.Record;
import com.myspace.pojo.User;
import com.myspace.service.space.account.GetUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GetUserInfoServiceImpl implements GetUserInfoService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RecordMapper recordMapper;

    @Override
    public Map<String, String> getUserInfo(Integer myId, Integer userId) {
        User user = userMapper.selectById(userId);
        Map<String, String> map = new HashMap<>();

        map.put("id", user.getId().toString());
        map.put("username", user.getUsername());
        map.put("photo", user.getPhoto());
        map.put("followerCount", user.getFollowerCount().toString());

        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follow_id", myId);
        List<Record> records = recordMapper.selectList(queryWrapper);
        if(records.size() > 0) {
            for (Record record: records) {
                if(userId.equals(record.getTargetId())) {
                    map.put("is_followed", "true");
                    return map;
                }
            }
            map.put("is_followed", "false");
            return map;
        }else if (records.size() == 0) {
            map.put("is_followed", "false");
            return map;
        }
        return map;
    }
}
